package com.cn.qgx428.lazydiary.ui.fragment


import android.support.v4.app.Fragment
import android.view.View
import com.cn.qgx428.lazydiary.Presenter.UserPresenter

import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LActionWacher
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.network.Imageloader
import com.cn.qgx428.lazydiary.ui.activity.LoginActivity
import com.cn.qgx428.lazydiary.ui.activity.SyncActivity
import com.cn.qgx428.lazydiary.ui.activity.subActivity.ResetPwdActivity
import com.cn.qgx428.lazydiary.ui.activity.subActivity.UserInfoActivity
import kotlinx.android.synthetic.main.fragment_mine.*
import org.jetbrains.anko.startActivity


/**
 * A simple [Fragment] subclass.
 */
class MineFragment : BaseFragment(), LActionWacher {

    override fun onTrigger(action: String, msg: LAMessege) {
        when(action){
            LAction.ACTION_LOGIN->{
                switchMainView(false)
                initUserView()
                //getUserInfoFromNet()
            }
            LAction.ACTION_UPDATE_USER_INFO->{
                getUserInfoFromNet()
            }
        }
    }

    override fun initView() {
        LManager.newInstance().register(this,LAction.ACTION_LOGIN)
        LManager.newInstance().register(this,LAction.ACTION_UPDATE_USER_INFO)

        mine_lgCircle.setOnClickListener({context.startActivity<LoginActivity>()
            activity.overridePendingTransition(R.anim._loginactivity_in, R.anim._activity_conteract)
        })
        // 检查登陆状态
        if(SPUtil.checkUserLogin()){
            switchMainView(false)
            //
            initUserView()
        } else{
            switchMainView(true)
        }
    }

    /**
     * 初始化用户数据
     */
    private fun initUserView() {
        minef_userName.text = SPUtil.get().getString("nickName","unknown")
        minef_logout.setOnClickListener({
            SPUtil.userLogOut()
            switchMainView(true)
        })
        minef_completeInfo.setOnClickListener({

            context.startActivity<UserInfoActivity>(
                    "userId" to SPUtil.currntUserId()
            )
        })
        minef_sync.setOnClickListener { context.startActivity<SyncActivity>() }

        minef_resetPwd.setOnClickListener{ context.startActivity<ResetPwdActivity>() }
        //
        getUserInfoFromNet()
    }


    private fun getUserInfoFromNet() {
        UserPresenter.getUserInfo(SPUtil.currntUserId().toInt(), {
            Imageloader.download(activity, minef_userImg, it.image)
            minef_userName.text = it.nickname
        })
    }

    private fun refreshUserInfo() {

    }

    override fun getLayoutId() = R.layout.fragment_mine

    /**
     * 切换主要视图
     */
    fun switchMainView(isLoginGuide: Boolean){
        if (isLoginGuide){
            minef_loginLayout.visibility = View.VISIBLE
            minef_mineLayout.visibility = View.GONE
        } else{
            minef_loginLayout.visibility = View.GONE
            minef_mineLayout.visibility = View.VISIBLE
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LManager.newInstance().unRegister(this)
    }
}// Required empty public constructor
